/*
 ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1

 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Blazing Games Inc. (Billy D. Spelchan) code.
 *
 * The Initial Developer of the Original Code is
 * Blazing Games Inc.
 * Portions created by the Initial Developer are Copyright (C) 1999, 2003, 2005-2006
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 *
 ***** END LICENSE BLOCK *****
 */
package com.blazinggames.holeman;

import java.awt.*;
import java.awt.event.*;
//import java.util.*;

public class Construct
extends Panel
implements MouseListener, MouseMotionListener {
// *** CONSTANTS ***
//  private final static int TILEBAR_Y = 440-32;

// *** VARIABLES ***
	ActionListener eventActionListener;

  Map map;
  Image iTiles;
  Image iExit;

  int nScreenWidth, nScreenHeight, nTileWidth, nTileHeight;

  int nCurTile, nLevel;

  boolean bIsDrawing;

// *** CONSTRUCTOR ***
 public Construct(Map m, ActionListener al) {
    screenHigh();
    map = m;
    addMouseListener(this);
    addMouseMotionListener(this);
    eventActionListener = al;

    requestFocus();
    nLevel = 0;
  }

  void screenHigh(){
    Toolkit tk = Toolkit.getDefaultToolkit();
    iTiles = tk.getImage("res/holeman/tiles.jpg");
    iExit = tk.getImage("res/holeman/exit.jpg");
    nScreenWidth = 640;
    nScreenHeight = 440;
    nTileWidth = nTileHeight = 32;
    System.gc();
  }

  void screenLow() {
    Toolkit tk = Toolkit.getDefaultToolkit();
    iTiles = tk.getImage("res/holeman/stiles.jpg");
    iExit = tk.getImage("res/holeman/sexit.jpg");

    nScreenWidth = 320;
    nScreenHeight = 220;
    nTileWidth = nTileHeight = 16;
    System.gc();
  }

// *** DISPLAY METHODS ***
  public synchronized void paint(Graphics g) {
    int temp;

    for (int cntrY = 0; cntrY < 12; ++cntrY)
      for(int cntrX = 0; cntrX < 20; ++cntrX) {
        temp = map.getSetTile(nLevel, cntrX, cntrY);
        g.drawImage(iTiles, cntrX * nTileWidth, cntrY * nTileHeight,
              cntrX * nTileWidth + nTileWidth, cntrY * nTileHeight + nTileHeight,
              temp * nTileWidth, 0, temp*nTileWidth+nTileWidth, nTileHeight,
              this);
      }

    Color oldColor = g.getColor();
    g.setColor(Color.white);
    g.fillRect(0, nTileHeight*12+1, nScreenWidth, nScreenHeight-12*nTileHeight);
    g.drawImage(iTiles, 0, 12*nTileHeight+(nTileHeight>>1), this);
    g.setColor(Color.blue);
    g.fillRect(nCurTile*nTileWidth,12*nTileHeight+2, nTileWidth, (nTileHeight>>1)-2);
    g.drawImage(iExit, 16*nTileWidth, 12*nTileHeight+(nTileHeight>>1), this);
    g.setColor(oldColor);
  }

  public void update(Graphics g) {
    paint(g);
  }

  public void setLevel(int nLvl) {
    nLevel = nLvl;
  }


// *** METHODS - EDITING ***
  /*
    Used for editing.  x and y are mouse coordinates.  If those coordinates
    happen to be on the editing area, the tile the mouse is over is changed
    to the current tile and the screen refreshed.  Note, if the tile value
    was the same as the new value, no refresh is performed.
  */
  private void changeTile(int x, int y) {
    int nX = x / nTileWidth;
    int nY = y / nTileHeight;
    if ( (nX < 20) && (nY < 12) ) {
      int nTile = map.getSetTile(nLevel, nX, nY);
      if (nTile != nCurTile) {
        map.setSetTile(nLevel, nX, nY, nCurTile);
        repaint();
      }
    }
  }

// *** MOUSE EVENTS ***
  /**
    Mouse Clicked event from MouseListener.
    Invoked when the mouse has been clicked on a component.
  */
  public void mouseClicked(MouseEvent e) { /* ignore */ }

  /**
    mousePressed event from MouseListener.
    Invoked when a mouse button has been pressed on a component.
  */
  public void mousePressed(MouseEvent e) {
    int nX = e.getX() / nTileWidth;
    int nY = e.getY() / nTileHeight;
    if ( (nX < 20) && (nY < 12) )
      bIsDrawing = true;

  }

 /**
    mouseReleased event from MouseListener.
    Invoked when a mouse button has been released on a component.
  */
  public void mouseReleased(MouseEvent e) {
    bIsDrawing = false;
    if (e.getY() > (12*nTileHeight)) {
      nCurTile = e.getX() / nTileWidth;
      if (nCurTile > 15) {
        nCurTile = 0;
  			eventActionListener.actionPerformed(
              new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "List"));
      }
    }
    int nX = e.getX() / nTileWidth;
    int nY = e.getY() / nTileHeight;
    if ( (nX < 20) && (nY < 12) )
      map.setSetTile(nLevel, nX, nY, nCurTile);
    repaint();
  }

  /**
    mouseEntered event from MouseListener.
    Invoked when the mouse enters a component.
  */
  public void mouseEntered(MouseEvent e) { /* ignore */ }

 /**
    mouseExited event from MouseListener.
    Invoked when the mouse exits a component.
  */
  public void mouseExited(MouseEvent e) { /* ignore */ }

  /**
    mouseDragged event from MouseMotionListener.
    Invoked when a mouse button is pressed on a component and then dragged.
    Mouse drag events will continue to be delivered to the component where the
    first originated until the mouse button is released (regardless of whether
    the mouse position is within the bounds of the component).
  */
  public void mouseDragged(MouseEvent e) {
    if (bIsDrawing)
      changeTile(e.getX(), e.getY());
  }

  /**
    mouseMoved event from MouseMotionListener.
    Invoked when the mouse button has been moved on a component (with no buttons no down).
  */
  public void mouseMoved(MouseEvent e) {
    /* ignore */
  }

}
